<?php
/*由bean脚本生成 powerBy--joe*/
namespace app\common\manage;

use app\common\bean\CategoryBean;
use app\common\bean\CategoryGroupBean;
use app\common\enum\CategoryTypeEnum;
use app\common\enum\ContentTypeEnum;
use app\common\exception\MyException;
use app\common\model\CategoryGroupModel;
use app\common\model\CategoryModel;

class CategoryManage
{
    static $_self = null;
    public static function getInstance(){
        if(empty(self::$_self)){
            self::$_self = new CategoryManage();
        }
        return self::$_self;
    }
    public function getCateName($cids){
        $arr = json_decode($cids, true);
        $str = '';
        foreach ($arr as $a){
            $info = CategoryModel::getInstance()->selectData([CategoryBean::$id=>$a]);
            $str .= $info->name.'+';
        }
        return substr($str, 0, -1);
    }
    public function delCate(CategoryBean $categoryBean){
        $where[] = [CategoryGroupBean::$categoryIds, 'LIKE', '['.$categoryBean->getId().',%'];
        $where[] = [CategoryGroupBean::$categoryIds, 'LIKE', '%,'.$categoryBean->getId().']'];
        $where[] = [CategoryGroupBean::$categoryIds, '=', '['.$categoryBean->getId().']'];
        CategoryGroupModel::getInstance()->whereOr($where)->delete();
        CategoryModel::getInstance()->where([CategoryBean::$id=>$categoryBean->getId()])->delete();
    }
    public function editCate(CategoryBean $categoryBean){
        $filed = CategoryBean::$id;
        $id = $categoryBean->getId();
        $ufiled = $categoryBean->getParameter('field');
        $fvalue = $categoryBean->getParameter('value');
        CategoryModel::getInstance()->updateColumn($filed, $id, $ufiled, $fvalue);
    }
    public function addCate(CategoryBean $categoryBean){
        $name = CategoryTypeEnum::getName($categoryBean->getType());
        $content_type = $categoryBean->getParameter('content_type');
        $data[CategoryBean::$storeId] = $categoryBean->getStoreId();
        $data[CategoryBean::$type] = $categoryBean->getType();
        $data[CategoryBean::$name] = $name;
        $data[CategoryBean::$price] = 10;
        $data[CategoryBean::$createdAt] = date('Y-m-d H:i:s');
        $si_id = CategoryModel::getInstance()->insertGetId($data);
        $spdata[] = [
            CategoryGroupBean::$storeId=>$categoryBean->getStoreId(),
            CategoryGroupBean::$categoryIds=>json_encode([$si_id], JSON_NUMERIC_CHECK),
            CategoryGroupBean::$type=>$content_type,
            CategoryGroupBean::$price=>10,
            CategoryGroupBean::$vipPrice=>7,
            CategoryGroupBean::$createdAt=>date('Y-m-d H:i:s')
        ];
        $cate_arr = $this->cateArr();
        $cate_info = $cate_arr[$content_type];
        if(count($cate_info)>count($spdata)){
            $spdata = [];
            unset($cate_info[$categoryBean->getType()]);
            $where[CategoryGroupBean::$type] = key($cate_info);
            $where[CategoryGroupBean::$storeId] = $categoryBean->getStoreId();
            $cate_items = CategoryModel::getInstance()->selectData($where, true);
            foreach ($cate_items as $ci){
                $arr = [$ci[CategoryBean::$id], $si_id];
                asort($arr);
                $spdata[] = [
                    CategoryGroupBean::$storeId=>$categoryBean->getStoreId(),
                    CategoryGroupBean::$categoryIds=>json_encode($arr, JSON_NUMERIC_CHECK),
                    CategoryGroupBean::$type=>$content_type,
                    CategoryGroupBean::$price=>10,
                    CategoryGroupBean::$vipPrice=>7,
                    CategoryGroupBean::$createdAt=>date('Y-m-d H:i:s')
                ];
            }
        }
        CategoryGroupModel::getInstance()->insertAll($spdata);
        return [$si_id, $name];
    }
    public function cateArr(){
        return [
            ContentTypeEnum::DEVPHOTO=>[
                CategoryTypeEnum::DEVPHOTOXZ=>CategoryTypeEnum::getName(CategoryTypeEnum::DEVPHOTOXZ),
                CategoryTypeEnum::DEVPHOTOCC=>CategoryTypeEnum::getName(CategoryTypeEnum::DEVPHOTOCC)
            ],
            ContentTypeEnum::CERPHOTO=>[
                CategoryTypeEnum::CERPHOTOLX=>CategoryTypeEnum::getName(CategoryTypeEnum::CERPHOTOLX)
            ],
            ContentTypeEnum::PRIPHOTO=>[
                CategoryTypeEnum::PRIPHOTOSC=>CategoryTypeEnum::getName(CategoryTypeEnum::PRIPHOTOSC),
                CategoryTypeEnum::PRIPHOTODX=>CategoryTypeEnum::getName(CategoryTypeEnum::PRIPHOTODX)
            ]
        ];
    }
    /** 根据类型获取处理后的数据
     * @param $storeId
     * @param $type
     * @return array
     * @throws \Exception
     */
    public function getDealData($storeId,$type){
        if(!in_array($type,[CategoryTypeEnum::DEVPHOTOXZ,CategoryTypeEnum::DEVPHOTOCC,CategoryTypeEnum::CERPHOTOLX,CategoryTypeEnum::PRIPHOTOSC,CategoryTypeEnum::PRIPHOTODX])){
            MyException::throwMyException("类型不存在");
        }
        $result = CategoryModel::getInstance()->selectData([CategoryBean::$storeId => $storeId,CategoryBean::$type => $type],true);
        $result !== false ?: MyException::throwMyException("获取类型失败");
        return $result;
//        return $this->dealDataToPhotoCategory($result);
    }

    /** 处理店家对应类型数据（外层开事务）
     * @param $names
     * @param $prices
     * @param $storeId
     * @param $type
     * @throws \Exception
     */
    public function dealArrayDatas($names,$prices,$storeId,$type){
        if(count($names) !== count($prices) || !$names){
            MyException::throwMyException("类型必须");
        }
        $categoryBean = new CategoryBean();
        $categoryBean->setStoreId($storeId);
        $categoryBean->setType($type);
        $dates = [];
        foreach ($names as $key => $value){
            $categoryBean->setName($value);
            $categoryBean->setPrice($prices[$key]);
            array_push($dates,$categoryBean->getData());
        }
        $result = CategoryModel::getInstance()->deleteData([CategoryBean::$storeId => $storeId,CategoryBean::$type => $type]);
        $result !== false ?: MyException::throwMyException("删除出错");
        CategoryModel::getInstance()->insertAll($dates);
    }

    /**
     * 组装动态照片类型数据
     * @param $dataList
     * @return array
     */
    public function dealDataToPhotoCategory($dataList){
        $datas = [];
        $categoryBean = new CategoryBean();
        foreach ($dataList as $key => $value){
            $categoryBean->setData($value);
            array_push($datas,['name' => $categoryBean->getName(),'price' => $categoryBean->getPrice()]);
        }
        return $datas;
    }
}
